<?php
namespace app\custom_user\logic;

use app\custom_user\model\UserModel;
use app\custom_user\model\MobileCodeModel;
use app\custom_user\model\UserSession;
use app\custom_user\validate\UserValidate;
use think\Db;

class UserLogic
{
    protected $error="";
    public function getError()
    {
        return $this->error;
    }

    //注册用户
    public function register($data)
    {
        $validate=new UserValidate();
        if(!$validate->scene("register")->check($data)){
            $this->error=$validate->getError();
            return false;
        }
        $um=new UserModel();
        $user=$um->createUser($data);
        if(!$user){
            $this->error=$um->getError();
            return false;
        }
        UserSession::set($user);
        return $user;
    }
    //删除用户
    public function remove($id)
    {
        $um=new UserModel();
        $user=$um->where(["id"=>$id])->find();
        if($user&&isset($user["token"])){
            UserSession::ban($user["token"]);
        }
        Db::table("ue_user")->where(["id"=>$id])->delete();
        Db::table("ue_user_outside")->where(["user_id"=>$id])->delete();
        UserSession::clear();
        return $this;
    }
    //登录
    public function login($data)
    {
        $validate=new UserValidate();
        if(!$validate->scene("login")->check($data)){
            $this->error=$validate->getError();
            return false;
        }
        
        // $token=UserSession::token();
        $where=["username"=>$data["username"]];
        $password=$data["password"];
        $um=new UserModel();
        $user=$um->checkUser($where,$password);
        if(!$user){
            $this->error=$um->getError();
            return false;
        }
        $um->loginUser($user);
        UserSession::set($user);
        return $user;
    }
    //修改用户信息(基本信息)
    public function updateInfo($user,$data)
    {
        $um=new UserModel();
        $result = $um->updateProfile($user["id"],$data);
        if($result===false){
            $this->error=$um->getError();
            return false;
        }
        $user=array_merge($user,$data);
        UserSession::set($user);
        return $this;
    }
    //修改用户手机号
    public function updateMobile($user,$data)
    {
        $validate=new UserValidate();
        if(!$validate->scene("update_mobile")->check($data)){
            $this->error=$validate->getError();
            return false;
        }
        
        $mobile=$data["mobile"];
        $code=$data["code"];
        $mcm=new MobileCodeModel;
        $result=$mcm->check($mobile,$code,"bind mobile");
        if(!$result){
            $this->error=$mcm->getError();
            return false;
        }
        $um=new UserModel();
        if(!$um->updateMobile($user["id"],$mobile)){
            $this->error=$um->getError();
            return false;
        }
        $user["mobile"]=$mobile;
        UserSession::set($user);
        return $this;
    }
    //修改用户密码
    public function updatePassword($user,$data)
    {
        $validate=new UserValidate();
        if(!$validate->scene("update_password")->check($data)){
            $this->error=$validate->getError();
            return false;
        }
        $um=new UserModel();
        if(!$um->updatePassword($user,$data["old_password"],$data["new_password"])){
            $this->error=$um->getError();
            return false;
        }
        return $this;
    }
    //找回密码
    public function getbackPassword($data)
    {
        $validate=new UserValidate();
        if(!$validate->scene("getback_password")->check($data)){
            $this->error=$validate->getError();
            return false;
        }
        $um=new UserModel;
        $user=$um->where(["username"=>$data["username"]])->find();
        if(!$user){
            $this->error="用户账号错误";
            return false;
        }
        $mobile=$user["mobile"];
        $mcm=new MobileCodeModel;
        $result=$mcm->check($mobile,$data["code"],"findback");
        if(!$result){
            $this->error=$mcm->getError();
            return false;
        }
        $password=$um->encryptPassword($data["new_password"],$user["create_time"]);
        $result = $um->save(["password"=>$password], ["id"=>$user["id"]]);
        if($result===false){
            $this->error="修改用户密码失败";
            return false;
        }
        return $this;
    }
    /**
    //查找用户
    public function getUserList($data)
    {
        $um=new UserModel();
        $query=$um->db();

        $pagesize=isset($data["pagesize"])?$data["pagesize"]:20;
        $pagenum=isset($data["pagenum"])?$data["pagenum"]:1;
        $query->page($pagenum,$pagesize);
        
        $equal_fields=["username"];
        foreach($data as $key => $item){
            if(in_array($key,$equal_fields)){
                $um->where("$key = '$item'");
            }
        }

        $like_fields=["nickname","mobile"];
        foreach($data as $key => $item){
            if(in_array($key,$like_fields)){
                $um->where("$key like '%$item%'");
            }
        }

        $list=$query->field("id,username,nickname,mobile,create_time")->select();
        return $list;
    }
     */
}
